/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.ErpOrderDetailPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  ErpOrderDetailPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class ErpOrderDetailSqlProvider {
	public static final String TABLE_NAME = "erp_order";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * @param orderDetail
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findErpOrderDetails(final ErpOrderDetailPo orderDetail) {
		return new CustomerSQL() {{
			SELECT("id,type,erp_order_id,ware_id,ware_name,price_original,number,create_time,creator_id,update_time,updater_id ");
			FROM(TABLE_NAME);
			if (null != orderDetail.getType()) {
				WHERE("type=#{type}");
			}
			if (null != orderDetail.getErpOrderId()) {
				WHERE("erp_order_id=#{erpOrderId}");
			}
			if (null != orderDetail.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderDetail.getWareName())) {
				WHERE("ware_name LIKE CONCAT('%',#{wareName},'%' ) ");
			}
			if(StringUtils.isNotBlank(orderDetail.getSortField())) {
				ORDER_BY(orderDetail.getSortField() + (StringUtils.isNotBlank(orderDetail.getSortMethod()) ? "desc" : orderDetail.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != orderDetail.getStart() && null != orderDetail.getSize()) {
				LIMIT(orderDetail.getStart(), orderDetail.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param orderDetail
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findErpOrderDetailCount(final ErpOrderDetailPo orderDetail) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != orderDetail.getType()) {
				WHERE("type=#{type}");
			}
			if (null != orderDetail.getErpOrderId()) {
				WHERE("erp_order_id=#{erpOrderId}");
			}
			if (null != orderDetail.getWareId()) {
				WHERE("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderDetail.getWareName())) {
				WHERE("ware_name LIKE CONCAT('%',#{wareName},'%' ) ");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findErpOrderDetailById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param orderDetail
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addErpOrderDetail(final ErpOrderDetailPo orderDetail) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != orderDetail.getType()) {
				VALUES("type","#{type}");
			}
			if (null != orderDetail.getErpOrderId()) {
				VALUES("erp_order_id","#{erpOrderId}");
			}
			if (null != orderDetail.getWareId()) {
				VALUES("ware_id","#{wareId}");
			}
			if (StringUtils.isNotBlank(orderDetail.getWareName())) {
				VALUES("ware_name","#{wareName}");
			}
			if (null !=orderDetail.getPriceOriginal()) {
				VALUES("price_original","#{priceOriginal}");
			}
			if (null !=orderDetail.getNumber()) {
				VALUES("number","#{number}");
			}
			if (null != orderDetail.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (null != orderDetail.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param orderDetail
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateErpOrderDetail(final ErpOrderDetailPo orderDetail) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			if (null != orderDetail.getType()) {
				SET("type=#{type}");
			}
			if (null != orderDetail.getErpOrderId()) {
				SET("erp_order_id=#{erpOrderId}");
			}
			if (null != orderDetail.getWareId()) {
				SET("ware_id=#{wareId}");
			}
			if (StringUtils.isNotBlank(orderDetail.getWareName())) {
				SET("ware_name=#{wareName}");
			}
			if (null !=orderDetail.getPriceOriginal()) {
				SET("price_original=#{priceOriginal}");
			}
			if (null !=orderDetail.getNumber()) {
				SET("number=#{number}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delErpOrderDetail(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 批量增加订单商品信息
	 * 
	 * @param map	
	 * @Author 迷蝶
	 * @return String
	 */
	public String insertBatch(final Map<String, List<ErpOrderDetailPo>> map) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        sb.append("INSERT INTO ").append(TABLE_NAME).append(" (type,erp_order_id,ware_id,ware_name,price_original,number,creator_id,updater_id) VALUES ");
        for (ErpOrderDetailPo detail : map.get("details")) {
        	if(i > 0) {sb.append(",");}
        	i++;
        	sb.append("(").append(detail.getType()).append(",")
        	.append(detail.getErpOrderId()).append("',")
        	.append(detail.getWareId()).append(",'")
        	.append(detail.getWareName()).append("',")
        	.append(detail.getPriceOriginal()).append(",")
        	.append(detail.getNumber()).append(",")
        	.append(detail.getCreatorId()).append(",'")
        	.append(detail.getUpdaterId()).append(",")
        	.append(")");
        }
        return sb.toString();
	}
}
